//detail->商品详情页的vuex
import { reqDetailInfo,reqActivityAndCoupon } from "@/api";

//state
const state = {
  detailInfo:{},
  couponInfoList:[],
  activityRuleList:[]
}

//mutations
const mutations = {
  //商品信息
  receive_detail_info(state,detailInfo){
    state.detailInfo = detailInfo
  },
  //优惠券和活动
  receive_couponList_activityList(state,{couponInfoList,activityRuleList}){
    state.couponInfoList = couponInfoList
    state.activityRuleList = activityRuleList
  }
}

//actions
const actions = {
  // 调用商品信息接口
  async reqDetailInfo({commit},skuId){
    const result = await reqDetailInfo(skuId)
    if(result.code === 200){
      commit('receive_detail_info',result.data)
    }
  },
  // 调用优惠券和活动列表接口
  async reqActivityAndCoupon({commit},skuId){
    const result = await reqActivityAndCoupon(skuId)
    if(result.code === 200){
      commit('receive_couponList_activityList',result.data)
    }
  }

}
//getters
const getters = {
  //详情页->面包屑
  categoryView(state){
    return state.detailInfo.categoryView || {}
  },
  //详情页->销售属性
  spuSaleAttrList(state){
    return state.detailInfo.spuSaleAttrList || []
  },
  //详情页->库存
  valuesSkuJson(state){
    return state.detailInfo.valuesSkuJson || '{}'
  },
  //详情页-> 商品数据
  skuInfo(state){
     return state.detailInfo.skuInfo || {}
  },
  //详情页 -> 轮播图
  skuImageList(state){
    const skuInfo = state.detailInfo.skuInfo
    return skuInfo ? skuInfo.skuImageList : {}
  }
}

export default{
  state,
  mutations,
  actions,
  getters
}